package com.yc.music.utils;

import ohos.agp.components.ComponentProvider;
import ohos.app.Context;
import ohos.event.notification.NotificationHelper;
import ohos.event.notification.NotificationRequest;
import ohos.event.notification.NotificationSlot;
import ohos.rpc.RemoteException;

import com.yc.music.ResourceTable;
import com.yc.music.model.AudioBean;
import com.yc.music.service.PlayService;

/**
 * NotificationManager
 *
 * @since 2021-05-10
 */
public class NotificationManager {
    private static final int NOTIFICATION_ID = 0x111;
    private PlayService playService;

    /**
     * NotificationManager
     */
    private NotificationManager() {
    }

    /**
     * NotificationManager
     *
     * @return return
     */
    public static NotificationManager get() {
        return SingletonHolder.instance;
    }

    /**
     * SingletonHolder
     *
     * @since 2021-05-10
     */
    private static class SingletonHolder {
        private static NotificationManager instance = new NotificationManager();
    }

    /**
     * 创建一个NotificationManager的引用
     *
     * @param playService playService
     */
    public void init(PlayService playService) {
        this.playService = playService;
    }

    /**
     * 开始播放
     *
     * @param music music
     */
    public void showPlay(AudioBean music) {
        if (music == null) {
            return;
        }
        playService.keepBackgroundRunning(NOTIFICATION_ID, buildNotification(playService, music, true));
    }

    /**
     * 暂停
     *
     * @param music music
     */
    public void showPause(AudioBean music) {
        // 这个方法是停止Notification
        if (music == null) {
            return;
        }
        playService.cancelBackgroundRunning();
        try {
            NotificationHelper.publishNotification(buildNotification(playService, music, false));
        } catch (RemoteException e) {
            MusicLogUtils.e(e.getMessage());
        }
    }

    /**
     * 结束所有的
     */
    public void cancelAll() {
        try {
            NotificationHelper.cancelAllNotifications();
        } catch (RemoteException e) {
            MusicLogUtils.e(e.getMessage());
        }
    }

    private NotificationRequest buildNotification(Context context, AudioBean music, boolean isPlaying) {
        NotificationUtils.setIsVibration(false);
        NotificationUtils notificationUtils = new NotificationUtils(context);
        notificationUtils
            .setPriority(NotificationSlot.LEVEL_DEFAULT)
            .setTicker("叮咚音乐")
            .setContent(getCustomViews(context, music, isPlaying))
            .setOngoing(true);
        NotificationRequest notification = notificationUtils.getNotification(
            music.getTitle(), music.getArtist(), ResourceTable.Media_default_cover);
        return notification;
    }

    /**
     * 设置自定义通知栏布局
     *
     * @param context 上下文
     * @param music music
     * @param isPlaying isPlaying
     * @return ComponentProvider
     */
    private ComponentProvider getCustomViews(Context context, AudioBean music, boolean isPlaying) {
        String title = music.getTitle();
        String subtitle = "";

        ComponentProvider componentProvider = new ComponentProvider();
        componentProvider.isValidComponentId(ResourceTable.Layout_notification_player);
        componentProvider.setImageContent(ResourceTable.Id_iv_image, ResourceTable.Media_default_cover);

        componentProvider.setText(ResourceTable.Id_tv_title, title);
        componentProvider.setText(ResourceTable.Id_tv_artist, subtitle);
        if (isPlaying) {
            componentProvider.setImageContent(ResourceTable.Id_btn_start,
                ResourceTable.Media_notify_btn_dark_pause_normal);
        } else {
            componentProvider.setImageContent(ResourceTable.Id_btn_start,
                ResourceTable.Media_notify_btn_dark_play_normal);
        }
        return componentProvider;
    }
}
